<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\cms\controller;

use plugin\saiadmin\basic\BaseController;
use app\cms\logic\MessageLogic;
use app\cms\validate\MessageValidate;
use support\Request;
use support\Response;

/**
 * 留言管理控制器
 */
class MessageController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new MessageLogic();
        $this->validate = new MessageValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['name', ''],
            ['status', ''],
        ]);
        $query = $this->logic->search($where);
        if (!empty($where['username'])) {
            $query->whereOr('phone', 'like', '%' . $where['name'] . '%')
                ->whereOr('email', 'like', '%' . $where['name'] . '%')
                ->whereOr('content', 'like', '%' . $where['name'] . '%');
        }
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    /**
     * 去处理
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-09
     * @param Request $request
     * @return void
     */
    public function auditMessageStatus(Request $request)
    {
        $id = $request->input('id', '');
        $result = $this->logic->where('id', $id)->update(['status' => 1, 'update_time' => date('Y-m-d H:i:s', time())]);
        if ($result) {
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    /**
     * 获取未读留言数量
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-09
     * @return void
     */
    public function getUnreadMessage()
    {
        return $this->success(['unread_message' => $this->logic->where('status', 0)->count()], '操作成功');
    }
}
